package com.insyte.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.google.gson.Gson;
import com.insyte.R;
import com.insyte.config.Constants;
import com.insyte.utils.Initialize;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class ForgotPasswordActivity extends ActionBarActivity {

    private AQuery aQuery;
    private EditText edt_email_reset;
    private Button btn_reset;
    String url_resetpass = "/password/reset/";
    private ProgressDialog progressDialog;
    private Initialize initialize;
    private Map<String, Object> params;
    String url_param;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);

        params = new HashMap<String, Object>();
        initialize = new Initialize(this);
        progressDialog = initialize.createProgressDialog("Loading");
        edt_email_reset = (EditText) findViewById(R.id.edt_email_reset);
        btn_reset = (Button) findViewById(R.id.btn_reset);
        aQuery = new AQuery(this);
        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//
                ResetPassword();
            }
        });
    }

    public void ResetPassword() {
        url_param = Constants.URL_API+url_resetpass + edt_email_reset.getText().toString();
        if (initialize.checkNetwork()) {
            progressDialog.show();
            aQuery.ajax(url_param, String.class, new AjaxCallback<String>() {
                @Override
                public void callback(String url, String result, AjaxStatus status) {
                    progressDialog.dismiss();
                    if (result != null) {
                        try {
                            JSONObject js = new JSONObject(result);
                            if (js.has("code")) {
                                if (js.getInt("code") == 1) {
                                    String jsMessage = js.getString("message");
                                    initialize.showToast(jsMessage);
                                    startActivity(new Intent(ForgotPasswordActivity.this,LoginActivity.class));
                                    finish();
                                } else {
                                    initialize.showToast(js.getString("message"));
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        initialize.showToast("check server");
                    }
                }
            });
        } else {
            initialize.showToast("Please check your network!");
        }
    }

}
